package com.library.view.components;

import com.library.controller.AdminBookController;
import com.library.pojo.dto.book.BookDTO;
import com.library.result.Result;
import io.yukk1o.easytermui.base.BasePanel;
import io.yukk1o.easytermui.base.Listener.InteractiveListener;
import io.yukk1o.easytermui.component.Button;
import io.yukk1o.easytermui.component.InputBox;
import io.yukk1o.easytermui.component.Panel;
import io.yukk1o.easytermui.component.Text;

import java.math.BigDecimal;

/**
 * 新增图书弹窗
 *
 * 包含组件:
 * 弹窗内：取消按钮、输入框（图书ID/书名/作者/出版社/价格/数量）、确认按钮
 *
 * 交互流程:
 * 1. 点击取消/按ESC → 关闭弹窗；
 * 2. 点击输入框可调整光标位置，点击确认按钮 → 向数据库新增图书，关闭弹窗并。
 */
public class BookAddDialog extends BasePanel {


    /**
     * 构造「嵌套Panel」（相对坐标）
     *
     * @param relX   相对于父Panel的X坐标
     * @param relY   相对于父Panel的Y坐标
     */
    public BookAddDialog(int relX, int relY, String title) {
        super(relX, relY, 60, 20);
        init(title);
    }

    public void init(String title){
        Panel tip = new Panel(25, 3, 10, 3);
        Text tipText = new Text(1, 1, 4, 1, "提示文本");
        tip.addComponent(tipText);
        addComponent(tip);
        Text titleText = new Text(26, 1, title.length(), 1, "新增图书");
        addComponent(titleText);

        /// 接收图书信息
        Text bookNameLabel = new Text(12, 3, 6, 1, "书名: ");
        InputBox bookNameInput = new InputBox(18, 3, 25);
        addComponent(bookNameLabel);
        addComponent(bookNameInput);

        Text authorLabel = new Text(12, 7, 6, 1, "作者: ");
        InputBox authorInput = new InputBox(18, 7, 25);
        addComponent(authorLabel);
        addComponent(authorInput);

        Text pressLabel = new Text(10, 11, 8, 1, "出版社: ");
        InputBox pressInput = new InputBox(18, 11, 25);
        addComponent(pressLabel);
        addComponent(pressInput);

        Text priceLabel = new Text(10, 14, 6, 1, "价格: ");
        InputBox priceInput = new InputBox(16, 14, 8);
        addComponent(priceLabel);
        addComponent(priceInput);

        Text numberLabel = new Text(31, 14, 6, 1, "数量: ");
        InputBox numberInput = new InputBox( 37, 14, 8);
        addComponent(numberLabel);
        addComponent(numberInput);

        Button addBtn = new Button(35, 17, " 添 加 ", (button -> {
            AdminBookController bookManageController = new AdminBookController();
            BookDTO bookDTO = new BookDTO();
            bookDTO.setName(bookNameInput.getValue());
            bookDTO.setAuthor(authorInput.getValue());
            bookDTO.setPublisher(pressInput.getValue());
            bookDTO.setPrice(new BigDecimal(priceInput.getValue() == null ? "0" : priceInput.getValue()));
            bookDTO.setStock(numberInput.getValue() == null ? null :  Integer.valueOf(numberInput.getValue()));
            bookDTO.setLoan(numberInput.getValue() == null ? null :  Integer.valueOf(numberInput.getValue()));
            if (bookDTO.getName() == null || bookDTO.getName().isEmpty() ||
                    bookDTO.getAuthor() == null || bookDTO.getAuthor().isEmpty() ||
                    bookDTO.getPublisher() == null || bookDTO.getPublisher().isEmpty() ||
                    bookDTO.getPrice() == null || bookDTO.getPrice().compareTo(BigDecimal.ZERO) <= 0 ||
                    bookDTO.getStock() == null || bookDTO.getStock() <= 0 ||
                    bookDTO.getLoan() == null || bookDTO.getLoan() <= 0) {
                tipText.setText("信息错误");
                tip.drawBorder();
                tip.render();

                clearTip(tipText, tip);
                return;
            }
            Result<String> result = bookManageController.insert(bookDTO);
            if (result.getCode() == 1) {
                InteractiveListener listener = (InteractiveListener) getBindData();
                listener.stopListener();
                return;
            }
            tipText.setText("未知错误");
            tip.drawBorder();
            tip.render();
            clearTip(tipText, tip);
        }));
        addComponent(addBtn);

        Button cancelBtn = new Button(15, 17, " 取 消 ", (button -> {
            InteractiveListener listener = (InteractiveListener) getBindData();
            listener.stopListener();
        }));
        addComponent(cancelBtn);
    }

    private void clearTip(Text tipText, Panel tip) {
        try {
            Thread.sleep(2000);
            tipText.setText("");
            tip.clear();
            render();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void renderContext() {
        drawBorder();
        super.renderContext();
    }
}
